// MessagesDialog.cpp
 
#include "StdAfx.h"

#include "Common/String/ResourceString.h"

#include "MessagesDialog.h"

#ifdef LANG
#include "Common/Lang/LangUtils.h"
#endif

using namespace Common;

#ifdef LANG
static Lang::CIDLangPair kIDLangPairs[] =
{
  { IDOK, 0x02000713 }
};
#endif

void CMessagesDialog::AddMessageDirect(LPCTSTR message)
{
  int itemIndex = _messageList.GetItemCount();
  TCHAR sz[32];
  _i64tot(itemIndex, sz, 10);
  _messageList.InsertItem(itemIndex, sz);
  _messageList.SetItemData(itemIndex, itemIndex);
  _messageList.SetItemText(itemIndex, 1, message);
}

void CMessagesDialog::AddMessage(LPCTSTR message)
{
  CString s = message;
  while (!s.IsEmpty())
  {
    int pos = s.Find(_T('\n'));
    if (pos < 0)
      break;
    AddMessageDirect(s.Left(pos));
    s.Delete(0, pos + 1);
  }
  AddMessageDirect(s);
}

LRESULT CMessagesDialog::OnInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
  #ifdef LANG
  Lang::LangSetWindowText(HWND(*this), 0x02000A00);
  Lang::LangSetDlgItemsText(HWND(*this), kIDLangPairs, sizeof(kIDLangPairs) / sizeof(kIDLangPairs[0]));
  #endif
  _messageList.Attach(GetDlgItem(IDC_MESSAGE_LIST));

  #ifndef UNDER_CE
  _messageList.SetUnicodeFormat(true);
  #endif

  _messageList.InsertColumn(0, _T(""), 30);

  const CString s =
    #ifdef LANG
    Lang::LangString(IDS_MESSAGES_DIALOG_MESSAGE_COLUMN, 0x02000A80);
    #else
    MyLoadString(IDS_MESSAGES_DIALOG_MESSAGE_COLUMN);
    #endif

  _messageList.InsertColumn(1, s, 600);

  for (size_t i = 0; i < Messages->size(); i++)
    AddMessage((*Messages)[i]);

  _messageList.SetColumnWidth(0, LVSCW_AUTOSIZE);
  _messageList.SetColumnWidth(1, LVSCW_AUTOSIZE);
  CenterWindow(GetParent());
  return TRUE;
}

LRESULT CMessagesDialog::OnSize(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM lParam, BOOL& /*bHandled*/)
{
  int xSize = LOWORD(lParam);
  int ySize = HIWORD(lParam); 

  int mx, my;
  RECT rect;
    rect.left = 0;
    rect.top = 0;
    rect.right = 8;
    rect.bottom = 8;
    MapDialogRect(&rect);
     
    mx = rect.right - rect.left;
    my = rect.bottom - rect.top;
   
  int bx, by;

  CWindow wndOK = GetDlgItem(IDOK);
  wndOK.GetWindowRect(&rect);
  bx = rect.right - rect.left;
  by = rect.bottom - rect.top;
  int y = ySize - my - by;
  int x = xSize - mx - bx;

  InvalidateRect(NULL);

  wndOK.MoveWindow(x, y, bx, by);
  _messageList.MoveWindow(mx, my, xSize - mx * 2, y - my * 2);
  return false;
}
